﻿/*  ----------------------------------------------------------------------------
 *  Warren Moxley
 *  ----------------------------------------------------------------------------
 *  GoogleHelper
 *  ----------------------------------------------------------------------------
 *  File:       TaskHelper.cs
 *  Author:     IT1SERVICES\wmoxley
 *  ----------------------------------------------------------------------------
	Licensed to the Apache Software Foundation (ASF) under one
       	or more contributor license agreements.  See the NOTICE file
       	distributed with this work for additional information
       	regarding copyright ownership.  The ASF licenses this file
       	to you under the Apache License, Version 2.0 (the
       	"License"); you may not use this file except in compliance
       	with the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

       	Unless required by applicable law or agreed to in writing,
       	software distributed under the License is distributed on an
       	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       	KIND, either express or implied.  See the License for the
       	specific language governing permissions and limitations
       	under the License. 
 *  ----------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Google.Apis;
using Google.Apis.Discovery;
using Google.Apis.Requests;
using GoogleRequests = Google.Apis.Requests;
using Google.Apis.Authentication;
using Google.Apis.Wrappers.Tasks;
using Data = Google.Apis.Wrappers.Tasks.Data;

using DotNetOpenAuth;
using DotNetOpenAuth.OAuth;
using System.Web;

namespace GoogleHelper
{
    public class TaskHelper
    {

        #region Private Variables

        string userName;
        string password;

        #endregion

        #region Constructors

        public TaskHelper(string userName, string password)
        {
            this.userName = userName;
            this.password = password;
        }

        #endregion

        #region List Methods 
        
        public Data.Tasks GetTasks(string taskListId, 
            string pageToken, bool showCompleted, bool showDeleted, bool showHidden)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasks tasks = new Tasks(svc);

            return tasks.ListAsObject(taskListId, "", "", "", "", 100, pageToken, showCompleted, showDeleted, showHidden, "");
            
        }

        #endregion

        #region Get Methods

        public Data.Task GetTask(string taskId, string taskListId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasks tasks = new Tasks(svc);

            return tasks.GetAsObject(taskId, taskListId);

        }

        #endregion

        #region Update Methods

        public Data.Task UpdateTask(Data.Task task, string taskId, string taskListId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasks tl = new Tasks(svc);

            return tl.UpdateAsObject(task, taskId, taskListId);
            
        }

        public Data.Task MoveTask(string taskId, string taskListId, string parentTaskId, string previousSiblingId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasks tl = new Tasks(svc);

            return tl.MoveAsObject("", taskId, taskListId, parentTaskId, previousSiblingId);
            
        }


        #endregion

        #region Insert Methods

        public Data.Task InsertTask(Data.Task task, string taskListId, string parentTaskId, string previousSiblingId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasks tl = new Tasks(svc);

            return tl.InsertAsObject(task, taskListId, parentTaskId, previousSiblingId);

        }

        #endregion

        #region Delete Methods

        public string DeleteTask(string taskId, string taskListId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasks tl = new Tasks(svc);

            return new StreamReader(tl.DeleteAsObject(taskId, taskListId)).ReadToEnd();

        }

        #endregion

    }

}
